package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	log "github.com/sirupsen/logrus"
	"ginweb/system"
	"gopkg.in/mgo.v2/bson"
	"fmt"
	"ginweb/system/mongodb"
	"ginweb/system/redis"
)

func Index(c *gin.Context)  {
	ConfigLocalFilesystemLogger(system.GetConfiguration().LogPath, "database.log", 7*24*time.Hour, 24*time.Hour)
	log.Error("我就测试下log路径")
	c.String(http.StatusOK, "我是common的index哦")
}

type User struct {
	Id_       bson.ObjectId `bson:"_id"`
	Name      string `bson:"name"`
	Age       int `bson:"age"`
	JoinedAt  time.Time `bson:"joned_at"`
	Interests []string `bson:"interests"`
}

func TestMongodb(content *gin.Context) {
	session := mongodb.CloneSession()//调用这个获得session
	defer session.Close()  //一定要记得释放

	c := session.DB("ginweb").C("people")
	err := c.Insert(&User{Id_: bson.NewObjectId(),
		Name: "Jimmy Kuu",
		Age: 33,
		JoinedAt: time.Now(),
		Interests: []string{"Develop", "Movie"} })

	if err != nil {
		panic(err)
	}

	var users []User
	err=c.Find(nil).Limit(5).All(&users)
	if err != nil {
		panic(err)
	}
	fmt.Println(users)
}

func TestRedis(c *gin.Context)  {
	client := redis.Init()
	err := client.Set("dsl","lxl",0).Err()
	if err != nil {
		panic(err)
	}
	val, err := client.Get("dsl").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(val)
}